package com.yanqu.road.dao.impl.activity.stronghold;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdUnionBase;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class StrongholdUnionBaseDaoImpl extends TempDataObjectDao<StrongholdUnionBase> {
    public static String getStrongholdUnionBaseSql = "select * from t_u_activity_stronghold_base where activityId=?";

    public Map<String,StrongholdUnionBase> getStrongholdUnionBase(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, StrongholdUnionBase> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getStrongholdUnionBaseSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdUnionBase strongholdUnionBase = parse(rs);
                dataMap.put(strongholdUnionBase.getUnionUid(), strongholdUnionBase);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateStrongholdUnionBaseSql = "update t_u_activity_stronghold_base set lv=?,curProcess=? where activityId=? and unionUid=?";

    @Override
    protected boolean updateData(StrongholdUnionBase strongholdUnionBase) {
        strongholdUnionBase.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdUnionBase.getLv()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdUnionBase.getCurProcess())));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdUnionBase.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdUnionBase.getUnionUid()));
        if (getDb().executeNoneQuery(updateStrongholdUnionBaseSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdUnionBaseSql = "insert into t_u_activity_stronghold_base(activityId,unionUid,lv,curProcess) values (?,?,?,?)";

    @Override
    protected boolean addData(StrongholdUnionBase strongholdUnionBase) {
        strongholdUnionBase.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdUnionBase.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdUnionBase.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdUnionBase.getLv()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdUnionBase.getCurProcess())));
        if (getDb().executeNoneQuery(insertStrongholdUnionBaseSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdUnionBase parseData(ResultSet rs) throws SQLException {
        StrongholdUnionBase strongholdUnionBase = new StrongholdUnionBase();
        strongholdUnionBase.setActivityId(rs.getInt("activityId"));
        strongholdUnionBase.setUnionUid(rs.getString("unionUid"));
        strongholdUnionBase.setLv(rs.getInt("lv"));
        strongholdUnionBase.setCurProcess(JSON.parseArray(rs.getString("curProcess"), Integer.class));
        return strongholdUnionBase;
    }
}
